//
//  FlSetViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/6.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "FlSetViewController.h"
#import "SetViewController.h"
#import "UserInfoManager.h"

@interface FlSetViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UIImageView *rightArrowImage;
@property (nonatomic, strong) UIButton *setButton;
@property (nonatomic, strong) UIButton *outButton;
@property (nonatomic, strong) UIAlertView *logoutAlertView;

@end

@implementation FlSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.topTitleView setDetailDic:@{Nav_Title:@"设置"}.mutableCopy];
	[self loadAllSubViews];
}

- (void)loginOut {
	[self.logoutAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		if (alertView == self.logoutAlertView) {                //退出登录
			[self logout];
		}
	}
}

- (void)logout {
	[UserInfoManager deleteWithPhone:UDPhone]; // 删除手机号保留下来的数据
	UDToken = nil;
	UDPhone = nil;
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)clickSetAction {
	SetViewController *set = [[SetViewController alloc] init];
	[self.navigationController pushViewController:set animated:YES];
}

- (void)loadAllSubViews{
	[self.view addSubviews:@[self.operView,self.leftLabel,self.rightArrowImage,self.setButton,self.outButton]];
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(15.0));
		make.height.equalTo(@(kQKFitModuleDecimal(50.0)));
	}];
	
	[self.leftLabel makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(15.0));
		make.centerY.equalTo(self.operView.mas_centerY);
	}];
	
	[self.rightArrowImage mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(15.0));
		make.centerY.equalTo(self.operView.mas_centerY);
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(16.0), kQKFitModuleDecimal(16.0)));
	}];
	
	[self.setButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.edges.equalTo(self.operView);
	}];
	
	[self.outButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(50.0)));
		make.top.equalTo(self.operView.mas_bottom).offset(kQKFitModuleDecimal(30));
	}];
}

#pragma mark----- 	lazy init -----

- (UIView *)operView {
	if (!_operView) {
		_operView = [[UIView alloc] init];
		_operView.backgroundColor = [UIColor whiteColor];
	}
	return _operView;
}

- (UILabel *)leftLabel {
	if (!_leftLabel) {
		_leftLabel = [UILabel labelWithText:@"个人资料" font:[UIFont systemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x333333)];
	}
	return _leftLabel;
}

- (UIImageView *)rightArrowImage {
	if (!_rightArrowImage) {
		_rightArrowImage = [[UIImageView alloc] init];
		_rightArrowImage.image = [UIImage imageNamed:@"arrow_right"];
	}
	return _rightArrowImage;
}

- (UIButton *)outButton {
	if (!_outButton) {
		_outButton = [UIButton buttonWithTitle:@"退出" color:[UIColor redColor] font:[UIFont systemFontOfSize:kQKFitFont(14)]];
		_outButton.backgroundColor = [UIColor whiteColor];
		[_outButton addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
	}
	return _outButton;
}

- (UIButton *)setButton {
	if (!_setButton) {
		_setButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_setButton addTarget:self action:@selector(clickSetAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _setButton;
}
- (UIAlertView *)logoutAlertView {
	if (!_logoutAlertView) {
		_logoutAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要退出账号吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
	}
	return _logoutAlertView;
}

@end
